package start;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

public class TestAuto {
		/** 
	   * 执行CMD命令,并返回String字符串? 
	   */
	public static String executeCmd(String strCmd) throws Exception {
	    System.out.println("cmd /c " + strCmd);
	    Process p = Runtime.getRuntime().exec("cmd /c " + strCmd); 
	    StringBuilder sbCmd = new StringBuilder();
	    //这里很重要，设置GB2312解决乱码！！！
	    //如果程序默认编码就是GB2312，可以不写
	    //我NetBeans默认用UTF8
	    BufferedReader br = new BufferedReader(new InputStreamReader(p 
	        .getInputStream(), "GB2312")); 
	    String line; 
	    while ((line = br.readLine()) != null) { 
	      sbCmd.append(line + "\n"); 
	    }
	    return sbCmd.toString(); 
	     
	    /*
	    //如果整个过程换成这样，就更清楚了。getInputStream是获取最原始的字节流，
	    //cmd返回的是以GB2312双字节编码的字节流
	    InputStream in = p.getInputStream();
	    byte[] b = new byte[2000];
	    in.read(b);
	    String msg = new String(b, "GB2312");
	    //用GB2312解释这堆字节，就可以组装成一个正常的String了
	    //如果上边不写GB2312，等于这里用UTF8组装，结果一样
	    return msg;
	    */
	  } 
	public static void main(String[] args) {
		 String dir = System.getProperty("user.dir");
//	  	  String Path = dir.replace("\\", "/");
	  	  String exePath = dir + "/startac.exe";
		try {
			changeStart(exePath);
			Thread t = new Thread(){
				public void run() {
					try {
						while(true){
							//判断主程序是否运行?
							if(!executeCmd("tasklist /fi \"IMAGENAME eq AutoClick.exe\"").contains("AutoClick.exe")){
								//运行主程序?
								 String dir = System.getProperty("user.dir");
//							  	  String Path = dir.replace("\\", "/");
							  	  String exePath = dir + "/deamon/AutoClick.exe";
							  	try{
							  		Runtime.getRuntime().exec(exePath);
							  	}catch(Exception e){
							  		e.printStackTrace();
							  	}
							}
							Thread.sleep(20000);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
			t.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	 public static void changeStart(String exePath) throws Exception{  
	       String regKey = "";
	      //获得操作系统名字，判断是windows的什么版本，这个可以根据需要加，该方法也可以判断是否是其他操作系统
		  String osName = System.getProperty("os.name");  
	      if(osName.equals("Windows 10")){
	    	  regKey = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
	      }else if(osName.equals("Windows 7")||osName.equals("Windows 8")){
	    	  regKey =  "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
	      }else if (osName.endsWith("Windows XP")) {
	    	  regKey = System.getProperty("user.home") + "\\「开始」菜单\\程序\\启动";
		  }else{
			  regKey =  "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
		  }
	      String myAppName = "MyAutoClick";  
	      Runtime runtime = Runtime.getRuntime();  
	      	//exePath="\"C:\\Program Files\\AutoClick\\AutoClick\\AutoClick.exe\"";
	    	  Process exec = runtime.exec("reg "+"add "+regKey+" /v "+myAppName+" /t reg_sz /d \""+exePath+"\" /f");
	      	System.out.println("reg "+"add "+regKey+" /v "+myAppName+" /t reg_sz /d \""+exePath+"\" /f");
	      	exec.getOutputStream().close();
	    	  runtime.exec("taskkill /f /im reg.exe");
	 }   
}
